package com.yan.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.yan.msmservice.service.MsmService;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(HashMap<String, Object> param, String phone) {
        if(StringUtils.isEmpty(phone)) return false;

        DefaultProfile profile =
                DefaultProfile.getProfile("default",
                        "LTAI4GCebwfuQsVtxyFwS4di", "TZ4tsUugTAWZMvqaoIDj8E8xq2IqSc");
        IAcsClient client = new DefaultAcsClient(profile);
//设置相关固定参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
//        设置发送相关参数
        request.putQueryParameter("PhoneNumbers", phone);//电话
        request.putQueryParameter("SignName", "在线教育网站使用");//签名名称
        request.putQueryParameter("TemplateCode", "SMS_204065044");//模版CODE
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//验证码，转换为json数据
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
            return false;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }

    }
}
